package org.ns.summer.aop;

import org.ns.summer.beans.FactoryBean;
import org.ns.summer.core.FieldUtils;

public class ProxyFactoryBean implements FactoryBean {

    private Object target;
    private MethodInterceptor interceptor;
    private Class<?> objectType = null;
    private Object object;

    public void setTarget(Object target) {
        this.target = target;
        this.objectType = AopUtils.getTargetType(target.getClass());
    }

    public void setInterceptor(MethodInterceptor interceptor) {
        this.interceptor = interceptor;
    }

    @Override
    public Class<?> getObjectType() {
        return objectType;
    }

    @Override
    public Object getObject() {
        if (object == null) {
            MethodInvokeInterceptor methodInvokeInterceptor = new MethodInvokeInterceptor(target, objectType, interceptor);
            object = CglibProxyUtils.proxy(objectType, methodInvokeInterceptor);
            FieldUtils.copyFieldValue(objectType, target, object);
        }
        return object;
    }
}
